package com.hzit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hzit.entity.Classes;
import com.hzit.entity.Student;
import com.hzit.mapper.ClassesMapper;
import com.hzit.mapper.StudentMapper;
import com.hzit.service.StudentService;
import com.hzit.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Service
public class StudentServiceImpl implements StudentService {
	@Autowired
	private StudentMapper studentMapper;
	@Autowired
	private ClassesMapper classesMapper;

	//1. 查询学生列表
	@Override
	public List<StudentVo> findAll() {
		List<StudentVo> studentVos = new ArrayList<>();
		//1.1 开始查询学生列表
		List<Student> students = studentMapper.selectByExample(null);
		//1.2 遍历上面的学生集合，将其转换为List<StudentVo>集合
		for (Student student : students) {
			//1.2.1 构造Vo对象
			StudentVo vo = new StudentVo();
			//1.2.2 复制student对象的属性值到vo对象中
			BeanUtil.copyProperties(student,vo,true);
			//1.2.3 为vo对象的cname字段进行赋值
			Classes classes = classesMapper.selectByPrimaryKey(student.getCid());
			String cname = classes.getCname();
			vo.setCname(cname);
			//1.2.4 将vo放到studentVos中
			studentVos.add(vo);
		}
		//1.3 返回
		return studentVos;
	}
}
